<template>
  <el-row :gutter="20">
    <el-col :span="7">
      <el-card>
        <el-form
          :rules="rules"
          :inline="true"
          ref="conncectFormRef"
          :model="connectForm"
          class="form-contain"
          size="small"
        >
          <el-form-item
            :label="t('ptl.port')"
            prop="port"
          >
            <el-input
              type="number"
              v-model.number="connectForm.port"
              :disabled="ptlServerStatus"
              :placeholder="t('ptl.serverPort')"
              @keyup.enter="connect(conncectFormRef)"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              :type="ptlServerStatus ? 'danger' : 'primary'"
              @click="connect(conncectFormRef)"
              :loading="loading"
            >
              {{ t(ptlServerStatus ? 'ptl.discreat' : 'ptl.creat') }}
            </el-button>
          </el-form-item>
        </el-form>

        <!-- 根据消息类型展示不同的表单 -->
        <el-tabs
          type="border-card"
          v-model="activeName"
          class="demo-tabs"
          @tab-click="handleClick"
        >
          <el-tab-pane
            label="KP"
            name="KP"
          >
            <KpMessage :port="connectForm.port" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </el-col>
    <el-col :span="17">
      <Log />
    </el-col>
  </el-row>
</template>

<script lang="ts" setup>
import {
  FormInstance,
  FormRules,
  TabsPaneContext,
} from 'element-plus'
import { reactive, ref } from 'vue'
import Log from '@/components/Log/ptlLog.vue'
import { closeServer, creatServer } from '@/api/ptl'
import { useI18n } from 'vue-i18n'
import { ptlStore } from '@/store/ptlStore'
import { storeToRefs } from 'pinia'
import KpMessage from './components/KpMessage.vue'

const { t } = useI18n()

const activeName = ref('KP')

const loading = ref(false)

const store = ptlStore()

const { ptlServerStatus, ptlServerPort } = storeToRefs(store) // 连接状态

// 连接信息表单
const connectForm = reactive({
  port: ptlServerPort.value,
})

// 表单校验规则
const rules = reactive<FormRules>({
  port: [
    {
      required: true,
      message: '请填写创建的端口',
      trigger: 'change',
    },
    { type: 'number', message: '端口号必须为数字类型' },
  ],
})

// 连接
const conncectFormRef = ref<FormInstance>()
const connect = async (formEl: FormInstance | undefined) => {
  // 储存port
  store.setPtlServerPort(connectForm.port)

  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = true
      if (ptlServerStatus.value) {
        // 断开连接
        closeServer()
          .then((res) => {
            store.setPtlServerStatus(false)
          })
          .finally(() => {
            loading.value = false
          })
      } else {
        // 建立连接
        creatServer(connectForm.port)
          .then((res) => {
            store.setPtlServerStatus(true)
          })
          .finally(() => {
            loading.value = false
          })
      }
    }
  })
}

const handleClick = (tab: TabsPaneContext, event: Event) => {
  // console.log(tab, event)
}
</script>
<style scoped lang="scss">
.form-contain {
  margin-bottom: 20px;
}
</style>
